import time

from taobao.extension import Link_database
# from TaobaoWeb_Project.taobao.extension import Link_database


class del_data(object):
    """删除数据类"""
    def __init__(self, user_id, goods_id, store_name):
        self.user_id = user_id
        self.goods_id = goods_id
        self.store_name = store_name
        self.con, self.obj = Link_database()

    def del_ItemAnalysis(self):
        """删除单品数据分析数据"""
        sql = """delete from itemanalysis where user=%d and goods_id=%d and store_name='%s'""" % (self.user_id, self.goods_id, self.store_name)

        self.obj.execute(sql)

    def del_ItemKeywords(self):
        """删除单品关键词数据"""
        sql = """delete from test_itemkeywords where user=%d and goods_id=%d and store_name='%s'""" % (self.user_id, self.goods_id, self.store_name)

        self.obj.execute(sql)

    def del_BindingGoods(self):
        """删除单品绑定商品数据"""
        sql = """delete from item_associated where user_id=%d and goods_id=%d and store_name='%s'""" % (self.user_id, self.goods_id, self.store_name)

        self.obj.execute(sql)

    def del_commit(self):
        """操作提交"""
        self.con.commit()

    def the_end(self):
        self.obj.close()
        self.con.close()


def del_category(user_id, start_str, check_str):
    """
    删除类目构数据
    爬虫失败删除已爬取的数据
    """
    con, obj = Link_database()
    start = int(time.mktime(time.strptime(start_str, '%Y-%m')))
    check = int(time.mktime(time.strptime(check_str, '%Y-%m')))
    # print(start)
    # print(check)
    sql = """delete from category_structure where user_id=%d and statdate between %d and %d""" % (user_id, check, start)
    obj.execute(sql)
    con.commit()

    obj.close()
    con.close()


if __name__ == '__main__':
    del_category(318, '2018-9', '2018-9')